﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// A query wrapper for the thrift call multiget_slice for a slice of supercolumns
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN">supercolumn type</typeparam>
    /// <typeparam name="N">column type</typeparam>
    public class ThriftMultigetSuperSliceCounterQuery<K, SN, N> : AbstractSliceQuery<K, SN, long, CounterSuperRows<K, SN, N>>, MultigetSuperSliceCounterQuery<K, SN, N>
    {
        public ThriftMultigetSuperSliceCounterQuery(Keyspace keyspace,
                                              Serializer<K> keySerializer,
                                              Serializer<SN> sNameSerializer,
                                              Serializer<N> nameSerializer)
            : base(keyspace, keySerializer, sNameSerializer, LongSerializer.Instance)
        {
            if (nameSerializer == null)
            {
                throw new NullReferenceException("Name serializer cannot be null");
            }
            NameSerializer = nameSerializer;
        }
        public List<K> Keys
        {
            get;
            set;
        }
        public Serializer<N> NameSerializer
        {
            get;
            set;
        }
        public List<SN> GetColumnNames()
        {
            return base.ColumnNames;
        }
        public MultigetSuperSliceCounterQuery<K, SN, N> SetKeys(List<K> keys)
        {
            Keys = keys;
            return this;
        }
        public override string ToString()
        {
            return "MultigetSuperSliceQuery(" + Keys + "," + base.ToStringInternal() + ")";
        }
        public new MultigetSuperSliceCounterQuery<K, SN, N> SetRange(SN start, SN finish, bool reversed, int count)
        {
            return (MultigetSuperSliceCounterQuery<K, SN, N>) base.SetRange(start, finish, reversed, count);
        }
        public new MultigetSuperSliceCounterQuery<K, SN, N> SetColumnNames(List<SN> columnNames)
        {
            return (MultigetSuperSliceCounterQuery<K, SN, N>) base.SetColumnNames(columnNames);
        }
        public new MultigetSuperSliceCounterQuery<K, SN, N> SetColumnFamily(string cf)
        {
            return (MultigetSuperSliceCounterQuery<K, SN, N>) base.SetColumnFamily(cf);
        }
        public override QueryResult<CounterSuperRows<K, SN, N>> Execute()
        {
            Func<KeyspaceService, CounterSuperRows<K, SN, N>> x = (KeyspaceService ks) =>
            {
                List<byte[]> keylist = new List<byte[]>();
                foreach (K key in Keys)
                {
                    keylist.Add(KeySerializer.ToBytes(key));
                }
                ColumnParent columnParent = new ColumnParent(ColumnFamilyName);
                Dictionary<K, List<CounterSuperColumn>> thriftRet = KeySerializer.FromBytesDictionary(
                    ks.MultigetCounterSuperSlice(keylist, columnParent, GetPredicate()));
                return new CounterSuperRowsImpl<K, SN, N>(thriftRet, KeySerializer, ColumnNameSerializer, NameSerializer);
            };
            return new QueryResultImpl<CounterSuperRows<K, SN, N>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<CounterSuperRows<K, SN, N>>(x)), this);
        }
    }
}
